/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.events.EventType;
import com.bourre.medias.video.VideoDisplay;

import fever.medias.video.VideoCuePoint;
import fever.medias.video.VideoCuePointEvent;
import fever.medias.video.VideoMetaData;
import fever.utils.Stringifier;

/**
 * {@code FvVideoDisplay } class.
 *
 * @author Romain Ecarnot
 */
class fever.medias.video.FvVideoDisplay extends VideoDisplay
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when a cue point is triggered. */
	public static var onCuePointEVENT : EventType = VideoCuePointEvent.onCuePointEVENT;
	
		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _oMetaData : VideoMetaData;
	private var _cueList : Array;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates instance using only {@code video} flash object reference.
	 */
	public static function buildVideoDisplay( video : Video ) : FvVideoDisplay
	{
		return new FvVideoDisplay( video["_parent"], video );
	}
	
	/**
	 * Constructor.
	 */
	public function FvVideoDisplay( mcHolder : MovieClip, videoObject : Video )
	{
		super( mcHolder, videoObject );
		
		_cueList = new Array();
	}
	
	/**
	 * Returns video metadata.
	 */
	public function getMetaData() : VideoMetaData
	{
		return _oMetaData;
	}
	
	/**
	 * Returns internal cue points defined in video ( if any )
	 */
	public function getCuePoints() : Array
	{
		return _cueList;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _load() : Void
	{
		super._load();
		
		_oNS["onCuePoint"] = Delegate.create( this, _onCuePoint );
	}
	
	/**
	 * Overrides Videodisplay method to add cue points support.
	 */
	private function _onMetaData ( metadata : Object ) : Void
	{
		_cueList = new Array();
		
		_oMetaData = VideoMetaData.create( metadata );
		
		_nDuration = parseInt( metadata.duration );
		
		if ( metadata.keyframes )
		{
			_aKTimes = metadata.keyframes.times;
			_aKFilePositions = metadata.keyframes.filepositions;
		}
		
		if( metadata.cuePoints )
		{	
			var l : Number = metadata.cuePoints.length;
			for( var i : Number = 0; i < l; i++ )
				_cueList.push(  VideoCuePoint.create( metadata.cuePoints[i] ) );
		}
		
		fireEventType( VideoDisplay.onMetaDataEVENT );
	}
	
	private function _onCuePoint( cuepoint : Object ) : Void
	{
		_oEB.broadcastEvent( 
			new VideoCuePointEvent( 
				onCuePointEVENT, 
				this, 
				VideoCuePoint.create( cuepoint ) )
		 );
	}
}